package org.herrdommel.wakemeup.app;

import org.herrdommel.wakemeup.R;
import org.herrdommel.wakemeup.control.AlarmController;
import org.herrdommel.wakemeup.model.Alarm;
import org.herrdommel.wakemeup.provider.AlarmContentProvider;
import org.herrdommel.wakemeup.util.DialogFactory;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CheckBox;
import android.widget.ListView;

public class AlarmListFragment extends ListFragment {

	/** The list views adapter */
	private SimpleCursorAdapter adapter;

	/** The fragments list view */
	private ListView lvAlarmList;

	private AlarmController controller;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		controller = new AlarmController(getActivity(), getLoaderManager());

		// Define what column binds to what ui element
		final String[] uiBindFrom = {Alarm.TIME, Alarm.NAME, Alarm.IS_ACTIVE};
		final int[] uiBindTo = {R.id.txtAlarmTime, R.id.txtAlarmName, R.id.chkAlarmActive};

		// Create the adapter that will be used for the list
		adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.alarm_list_entry, null,
				uiBindFrom, uiBindTo, 0);
		adapter.setViewBinder(new AlarmListViewBinder());
	}
	@Override
	public void onResume() {
		super.onResume();
		getLoaderManager().restartLoader(AlarmContentProvider.LOADER_ALARM_LIST, null, loaderCallbackAlarmList);
	}

	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lvAlarmList = getListView();
		registerForContextMenu(lvAlarmList);
		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(final ListView parent, final View view, final int position, final long id) {
		// Start the AlarmEditor for the clicked alarm
		final Intent editAlarm = new Intent(getActivity(), AlarmEditor.class);
		editAlarm.putExtra("alarmid", id);
		startActivity(editAlarm);
	}
	@Override
	public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		final MenuInflater inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.context_menu_alarm_list, menu);
	}

	@Override
	public boolean onContextItemSelected(final MenuItem item) {
		final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		final long alarmId = menuInfo.id;
		switch (item.getItemId()) {
			case R.id.cmnuDeleteAlarm :
				controller.handleAlarmDeletion(alarmId, loaderCallbackAlarmList);
				return true;
			case R.id.cmnuEditAlarm :
				startAlarmEditor(alarmId);
				return true;
			default :
				return false;
		}
	}

	/** Starts the AlarmEditor activity to edit an alarm */
	private void startAlarmEditor(final long alarmId) {
		final Intent editAlarm = new Intent(getActivity(), AlarmEditor.class);
		editAlarm.putExtra("alarmid", alarmId);
		startActivity(editAlarm);
	}

	/** Class implementing the ViewBinder to correctly display the active state */
	private class AlarmListViewBinder implements SimpleCursorAdapter.ViewBinder {
		@Override
		public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
			if (columnIndex == cursor.getColumnIndex(Alarm.IS_ACTIVE)) {
				final CheckBox checkBox = (CheckBox) view;
				checkBox.setChecked(cursor.getInt(columnIndex) > 0);
				return true;
			}
			return false;
		}
	}

	private final LoaderCallbacks<Cursor> loaderCallbackAlarmList = new LoaderCallbacks<Cursor>() {

		@Override
		public Loader<Cursor> onCreateLoader(final int id, final Bundle extras) {
			CursorLoader cursorLoader = null;
			switch (id) {
				case AlarmContentProvider.LOADER_ALARM_LIST :
					final String[] projection = {Alarm._ID, Alarm.NAME, Alarm.TIME, Alarm.IS_ACTIVE};
					cursorLoader = new CursorLoader(getActivity(), AlarmContentProvider.CONTENT_URI, projection,
							null, null, null);
					break;
				default :
					break;
			}
			return cursorLoader;
		}

		@Override
		public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
			if (cursor.isAfterLast()) {
				// If there are no alarms in the db, display a dialog
				getActivity().showDialog(DialogFactory.DIALOG_LIST_EMPTY);
			}
			adapter.swapCursor(cursor);
		}

		@Override
		public void onLoaderReset(final Loader<Cursor> loader) {
			adapter.swapCursor(null);
		}
	};

}
